package com.czwj.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.czwj.entities.Lettertext;
import com.czwj.entities.OperateResult;
import com.czwj.entities.UserInfo;
import com.czwj.service.LettertextService;
import com.czwj.service.UserInfoService;

@Controller
public class LetterAction{
	@Resource
	LettertextService lettertextService;
	
	@Resource
	UserInfoService userInfoService;
	
	@RequestMapping(value = "/getUserName",method = RequestMethod.GET)
	@ResponseBody
	public Map getUsername(HttpServletRequest request,HttpServletResponse response){
		// HttpSession session = request.getSession();
		// Object userid = session.getAttribute("key");
		// String username = userid.toString().substring(3, userid.toString().length());
		// UserInfo user = userInfoService.findByUserName(username);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		int userCompany = userInfo.getCompanyid();
		List<UserInfo> userlist = userInfoService.selectbycompanyrole(userCompany);
		Map<Integer,Object> result = new LinkedHashMap<Integer,Object>();
		for(int i = 0;i < userlist.size();i++){
			UserInfo userinlist = userlist.get(i);
			result.put(i,userinlist);
		}
		return result;
	}
	
	@RequestMapping(value = "/getAllUserName",method = RequestMethod.GET)
	@ResponseBody
	public Map getAllUserName(HttpServletRequest request,HttpServletResponse response){
		// HttpSession session = request.getSession();
		// Object userid = session.getAttribute("key");
		// String username = userid.toString().substring(3, userid.toString().length());
		// UserInfo user = userInfoService.findByUserName(username);
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		int userCompany = userInfo.getCompanyid();
		List<UserInfo> userlist = userInfoService.selectbycompany(userCompany);
		Map<Integer,Object> result = new LinkedHashMap<Integer,Object>();
		for(int i = 0;i < userlist.size();i++){
			UserInfo userinlist = userlist.get(i);
			if(userinlist.getRoleid() != null && userinlist.getRoleid() != 6){
				result.put(i,userinlist);
			}
		}
		return result;
	}
	
	@RequestMapping(value = "/insertmailbox.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult insertmailbox(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "title",required = false) String title,
			@RequestParam(value = "message",required = false) String message,
			@RequestParam(value = "recname",required = false) String recname){
		OperateResult result = new OperateResult();
		Lettertext newletter = new Lettertext();
		if(title != "" && message != ""){
			Date senddate = new Date();
			Subject currentUser = SecurityUtils.getSubject();
			UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
			int userCompany = userInfo.getCompanyid();
			message = this.inStr(message);
			newletter.setTitle(title);
			newletter.setMessage(message);
			newletter.setState(1);
			newletter.setSenddate(senddate);
			newletter.setCompanyid(userCompany);
			newletter.setRecname(recname);
			newletter.setSendid(0);
			int count = lettertextService.insertSelective(newletter,images);
			result.setSuccess(count > 0);
		}else{
			result.setSuccess(false);
		}
		return result;
	}
	
	@RequestMapping(value = "/sendmail.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult sendmailbox(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "title",required = false) String title,
			@RequestParam(value = "message",required = false) String message,
			@RequestParam(value = "recname",required = false) String recname){
		OperateResult result = new OperateResult();
		Lettertext newletter = new Lettertext();
		if(title != "" && message != ""){
			Date senddate = new Date();
			Subject currentUser = SecurityUtils.getSubject();
			UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
			int userCompany = userInfo.getCompanyid();
			message = this.inStr(message);
			newletter.setTitle(title);
			newletter.setMessage(message);
			newletter.setState(1);
			newletter.setSenddate(senddate);
			newletter.setCompanyid(userCompany);
			newletter.setRecname(recname);
			newletter.setSendid(userInfo.getId());
			int count = lettertextService.insertSelective(newletter,images);
			result.setSuccess(count > 0);
		}else{
			result.setSuccess(false);
		}
		return result;
	}
	
	public String inStr(String oldStr){
		oldStr = oldStr.replace("'","''");
		oldStr = oldStr.replace("<","&lt;");
		oldStr = oldStr.replace(">","&gt;");
		oldStr = oldStr.replace("\n","<br>");
		oldStr = oldStr.replace("\t"," &nbsp;&nbsp;&nbsp;");
		oldStr = oldStr.replace("\"","&quot;");
		return oldStr;
	}
}
